<?php

require_once("lib/xmlrpc.inc");
require_once("lib/xmlrpc_wrappers.inc");
require_once("lib/xmlrpcs.inc");

    
$t = new taghCompare("out/out.imslex.lex.txt","imslex.lex");
$t = new taghCompare("out/out.nzz.lex.txt","nzz.lex");
$t = new taghCompare("out/out.wiki.lex.txt","wiki.lex");
// $t = new taghCompare("test.txt","test");
// $t = new taghCompare("Beglückung");


class taghCompare{
	
	function __construct($file,$pre){
		
		$this->pre = $pre;
		
		$this->correct = array();
		$this->correctfalse = array();
		$this->wrong = array();
		$this->noderiv = array();
		
		$this->set = $this->getItems($file);
		foreach ($this->set as $index=>$item) {
			// echo $item[0]."\n";
			if($index%100 == 0)
				echo $item[0]."\n";
			$this->compare($item);
		}
		
		$this->write();

		// $this->xmlrpc($file);
		
		echo "Korrekt: ".count($this->correct)."\n";
		echo "Korrekt nicht analysiert: ".count($this->correctfalse)."\n";
		echo "Falsch: ".count($this->wrong)."\n";
		echo "Analysierte Nicht-Derivate: ".count($this->noderiv)."\n";

	}
	
	function write(){
		foreach(array("correct"=>"correct","correctfalse"=>"correctfalse","wrong"=>"wrong","noDeriv"=>"noderiv") as $file=>$var){
			$handle = fopen("tagh/tagh.".$this->pre.".".$file.".txt","w");
			foreach($this->{$var} as $item){
				if(!empty($item))
					fwrite($handle,$item[0]."\n".implode("\n",$item[1])."\n");
				else
					fwrite($handle,"???\n");
			}
			fclose($handle);
		}
	}
	
	function xmlrpc($item){
		
		$item = iconv("UTF-8", "ISO-8859-1//IGNORE", $item);
		
		$f=new xmlrpcmsg('dwds.login',array(new xmlrpcval('uni01','string'), new xmlrpcval('$dwds01','string'))); 
		$c=new xmlrpc_client("index.php", "194.95.188.36", 8050); 
		$r=$c->send($f);
		$session_id = php_xmlrpc_decode($r->value());

		$f=new xmlrpcmsg('dwds.processor.tomata.analyse',
			array(
				new xmlrpcval($session_id,'string'),
				new xmlrpcval($item,'string'),
				new xmlrpcval('tomata','string'),
				new xmlrpcval('text','string'),
				new xmlrpcval('csv','string'),
				new xmlrpcval('','string'),
				new xmlrpcval(0,'int')
			));
		$r=$c->send($f);
		$r = $r->value();
		if($r == ""){
			return "";
		}
		return iconv("ISO-8859-1//IGNORE", "UTF-8", php_xmlrpc_decode($r));
	}
	
	function compare($item){
		
		// Get Tagh Analyse
		// $content = shell_exec('python tagh_test.python '.str_replace($was, $wie, $item[0]));
		// $content = shell_exec('LANG=de_DE.UTF-8; export LANG; python tagh_test.python '.$item[0]);

		// $locale = 'de_DE.UTF-8';
		// putenv('LANG='.$locale);
		// $content = shell_exec(iconv(mb_detect_encoding($item[0]),"UTF-8",'python tagh_test.python '.$item[0]));
		// passthru('python tagh_test.python '.($item[0]),$content);
		$content = $this->xmlrpc($item[0]);
		// echo $content."\n";
		
		// Search for Dervivates
		if(preg_match(
			   '/\s+([A-ZÜÖÄa-züöä\=\|]+)\/([A-Z])\~([a-z]+)(\[NN[^\<\>0-9]+Number=sg([^\<\>0-9]+)\])/Ux'
		   		,$content,$match)){
			// look up the result in the analyses
			foreach($item[1] as $ana){
				if(preg_match('/^'.str_replace('=','',$match[1]).'.*\^'.$match[2].'.+\+'.strtoupper($match[3]).'\-Deriv/',$ana)){
					// echo "OK: ".$item[0]."\n";
					$this->correct[] = $item;
				} else {
// 					echo "WRONG: ".$item[0]."\n";
					$this->wrong[] = $item;
				}
			}
		} else {
			if($item[1][0] == "???"){
				// echo "OK FALSE: ".$item[0]."\n";
				$this->correctfalse[] = $item;
			} else {
//				echo "NOOP: ".$item[0]."\n";
				$this->noderiv[] = $item;
			}
		}
		
	}
	
	function getItems($file){
		$arr = array();
		
		echo $file."\n";
		
		if(!($handle = fopen($file,"r"))) die("Konnte Datei nicht öffnen!\n");
		$key = "";
		$items = array();
		while(!feof($handle)) {
			$line = trim(fgets($handle));
			if(preg_match('/^\!/',$line))
				continue;
			if(!preg_match('/[\^\+]+/',$line)){ // schließt Item
				if(!preg_match('/\?\?\?/',$line)){
					if(!empty($key)){
						$arr[] = array($key,$items);
					}
					$key = trim($line);					
				} else {
					$arr[] = array($key,array($line));
					$key = "";
				}
				$items = array();
			} else {
				$items[] = trim($line);
			}
			
		}
		return $arr;	
	}
	
}



?>